<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'viewAdmin.jsp' starting page</title>
<link href="../css/bootstrap-cerulean.css" rel="stylesheet">
<link href="../css/charisma-app.css" rel="stylesheet">
<style type="text/css">
body {
	padding-bottom: 40px;
}

.sidebar-nav {
	padding: 9px 0;
}
</style>
	<script src="http://lib.sinaapp.com/js/jquery/2.2.4/jquery-2.2.4.min.js"></script>
	<script src="../js/common/public.js"></script>
	<script src="../js/base64.js"></script>
	<script type="text/javascript">
        $(document).ready(function () {
            var urlMsg = parseUrl(window.location.search);
            $("#token").val(urlMsg.token);
            if (urlMsg.token != null) {
                var token = decode(urlMsg.token);
                token=token.substring(0,token.length-1);
            }
            var html = $.ajax({
                type: "GET",
                url: "http://localhost:1068/admin/findAllAdmin?token="+token,
                async: false
            }).responseText;
            var result = eval('(' + html + ')');
            if (result.code != 200) {
                alert(result.msg)
            } else {
                for (index in result.data){
                    addAdminTable(result.data[index].id,
                        result.data[index].userName,
                        result.data[index].password
                       );
                    if ( result.data[index].status== 0){
                        $("#adminStatus"+result.data[index].id+"").html(" <span class='label label-important'>锁定</span>");
                        $("#adminOperation"+result.data[index].id+"").append(" <a class='btn btn-success' href='javascript:void(0);' onclick='unlock(id="+result.data[index].id+",this);'>" +
                            "<i class='icon-edit icon-white'></i>" +
                            "解锁用户" +
                            "</a>");
					}else {
                        $("#adminStatus"+result.data[index].id+"").html(" <span class='label label-success'>启用</span>");

                    }
                    if ( result.data[index].access== 0){
                        $("#adminAccess"+result.data[index].id+"").html(" <span class='label label-important'>普通管理员</span>");
                    }else {
                        $("#adminAccess"+result.data[index].id+"").html(" <span class='label label-success'>超级管理员</span>");

                    }

                    if ( result.data[index].access== 0 && result.data[index].status== 1){
                        $("#adminOperation"+result.data[index].id+"").append(" <a class='btn btn-danger' id='"+result.data[index].id+"' href='javascript:void(0);' onclick='lock(id="+result.data[index].id+",this);'>\n" +
                            "<i class='icon-edit icon-white'></i>" +
                            "锁定用户" +
                            "</a>");
                    }

                    $('#editAdminInfo'+ result.data[index].id+'').attr('href','editAdmin.html?token='+urlMsg.token+'&userName='+result.data[index].userName);

                }
            }
        });
        function deleteAdmin(id,a) {
            if(window.confirm('删除操作不可逆，确定要删除吗？')){
                var urlMsg = parseUrl(window.location.search);
                $("#token").val(urlMsg.token);
                if (urlMsg.token != null) {
                    var token = decode(urlMsg.token);
                    token=token.substring(0,token.length-1);
                }
                var html = $.ajax({
                    type: "DELETE",
                    url: "http://localhost:1068/admin/deleteAdmin?token="+token+"&id="+id,
                    async: false
                }).responseText;
                var result = eval('(' + html + ')');
                if (result.code != 200) {
                    alert(result.msg)
                } else {
                    alert("删除成功！")
                    $(a).parent().parent().remove();
                }
            }else{
                return false;
            }
        }
        function lock(id,a) {
            if(window.confirm('请确认操作')){
                var urlMsg = parseUrl(window.location.search);
                $("#token").val(urlMsg.token);
                if (urlMsg.token != null) {
                    var token = decode(urlMsg.token);
                    token=token.substring(0,token.length-1);
                }
                var html = $.ajax({
                    type: "PUT",
                    url: "http://localhost:1068/admin/updateAdmin",
					data:{id:id,status:0,token:token},
                    async: false
                }).responseText;
                var result = eval('(' + html + ')');
                if (result.code != 200) {
                    alert(result.msg)
                } else {
                    window.location.reload();
                }
            }else{
                return false;
            }
        }
        function unlock(id,a) {
            if(window.confirm('请确认操作')){
                var urlMsg = parseUrl(window.location.search);
                $("#token").val(urlMsg.token);
                if (urlMsg.token != null) {
                    var token = decode(urlMsg.token);
                    token=token.substring(0,token.length-1);
                }
                var html = $.ajax({
                    type: "PUT",
                    url: "http://localhost:1068/admin/updateAdmin",
                    data:{id:id,status:1,token:token},
                    async: false
                }).responseText;
                var result = eval('(' + html + ')');
                if (result.code != 200) {
                    alert(result.msg)
                } else {
                    window.location.reload();
                }
            }else{
                return false;
            }
        }
        function addAdminTable(id,userName,password) {
            $("#adminList tbody").append("  <tr class='odd'>" +
                "<td class=' sorting_1'>"+userName+"</td>" +
                "<td class='center '>"+password+"</td>" +
                "<td class='center ' id='adminStatus"+id+"'>" +
                "</td>" +
                "<td class='center ' id='adminAccess"+id+"'>" +
                "</td>" +
                "<td class='center 'id='adminOperation"+id+"'>" +
                "<a class='btn btn-info' id='editAdminInfo"+id+"' href=''>" +
                "<i class='icon-edit icon-white'></i>  " +
                "编辑信息" +
                "</a>" +
                " <a class='btn btn-danger' href='javascript:void(0);' onclick='deleteAdmin(id="+id+",this);'>" +
                "<i class='icon-trash icon-white'></i> " +
                "删除用户" +
                "</a>" +
                "</td>" +
                "</tr>");

        }
	</script>
</head>

<body>
	<div class="container-fluid"
		style="padding-left:0px;padding-right:2px;">
		<div class="row-fluid">
			<div id="content" class="span10" style="width:100%;">
				<div class="row-fluid sortable">
					<div class="box span12">
						<div class="box-header well">
							<h2>
								<i class="icon-user"></i>管理员列表
							</h2>
						</div>
						<div class="box-content">
							<div id="DataTables_Table_0_wrapper" class="dataTables_wrapper">
								
								<table class="table table-striped table-bordered bootstrap-datatable datatable dataTable" id="adminList">
									<thead>
										  <tr>
										  <th class="sorting_asc"   rowspan="1" colspan="1"  style="width: 15%">用户名称</th>
										  <th class="sorting"   rowspan="1" colspan="1"  style="width: 25%">用户密码</th>
										  <th class="sorting"   rowspan="1" colspan="1"  style="width: 10%">用户状态</th>
										  <th class="sorting"   rowspan="1" colspan="1"  style="width: 10%">用户权限</th>
										  <th class="sorting"   rowspan="1" colspan="1"  style="width: 40%">操作</th>
										  </tr>
									 </thead>

									<tbody>
									
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

</body>
</html>
